import http from "@/api";
import qs from "qs";

//技术员问题
export const countProblem = params => {
  return http.post("/pages/problem!countProblem.action", qs.stringify(params), { loading: false });
};

/**
 * @name 问题
 */
// 查询问题
export const findProblemPager = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.post(`/pages/problem!findProblemPager.action`, qs.stringify(newobj));
};

// 保存问题
export const saveProblem = (params: {}, header: string, idsStr: string, serviceDir: string, kuozhanshuju: {}) => {
  console.log(kuozhanshuju);
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "approvalNo") {
        newobj[header + ".piv." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "templateType") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "templateName") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "templateId") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "isShowBorder") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "isNewForm") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "serviceDirId") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  if (kuozhanshuju != undefined) {
    Object.keys(kuozhanshuju).forEach(key => {
      newobj[header + ".attrVals['" + `${key}` + "']"] = `${kuozhanshuju[key]}`;
    });
  }

  if (idsStr == "") {
    idsStr = "?" + serviceDir;
  } else {
    idsStr += "&" + serviceDir;
  }
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.post("" + `/pages/problem!saveProblem.action` + idsStr, qs.stringify(newobj));
};
// 删除问题
export const deleteProblem = params => {
  return http.post(`/pages/problem!deleteProblem.action?` + params, qs.stringify({}));
};

// 编辑问题
export const editProblemBaseInfo = (params: {}, header: string, idsStr: string, serviceDir: string, kuozhanshuju: {}) => {
  console.log(kuozhanshuju);
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (`${key}` === "approvalNo") {
        newobj[header + ".piv." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "templateType") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "templateName") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "templateId") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "isShowBorder") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "isNewForm") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else if (`${key}` === "serviceDirId") {
        newobj["templateDTO" + "." + `${key}`] = `${params[key]}`;
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  }
  if (kuozhanshuju != undefined) {
    Object.keys(kuozhanshuju).forEach(key => {
      newobj[header + ".attrVals['" + `${key}` + "']"] = `${kuozhanshuju[key]}`;
    });
  }

  if (idsStr == "") {
    idsStr = "?" + serviceDir;
  } else {
    idsStr += "&" + serviceDir;
  }
  // return http.post<ResPage<User.ResUserList>>(PORT1 + `/user/list`, params);
  return http.post("" + `/pages/problem!editProblemBaseInfo.action` + idsStr, qs.stringify(newobj));
};

// 查询id
export const problemEdit = (params: string) => {
  return http.post<String>(`/pages/problem!problemEdit.action?eno=` + params, qs.stringify({})); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 查询附件
export const findAllEventAttachment = (params: string) => {
  return http.post<String>(
    `/pages/eventAttachment!findAllEventAttachment.action?eventAttachmentDto.eventType=itsm.problem&eventAttachmentDto.eno=` +
      params,
    qs.stringify({})
  ); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 查询自定义
export const findEventEav = (params: string) => {
  return http.post<String>(
    `/pages/eventEav!findEventEav.action?eventEavDTO.eventType=itsm.problem&eventEavDTO.eno=` + params,
    qs.stringify({})
  ); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
};

// 问题关联请求
export const findProblemRelatedRequest = params => {
  return http.post(`/pages/problem!findProblemRelatedRequest.action`, qs.stringify(params), { loading: false });
};

// 开启流程
export const openProcess = params => {
  return http.post(`/pages/problem!openProcess.action`, qs.stringify(params), { loading: false });
};
// 重新指派
export const assignTask = params => {
  return http.post(`/pages/problem!assignTask.action`, qs.stringify(params), { loading: false });
};

// 问题指派
export const problemAssign = params => {
  return http.post(`/pages/problem!problemAssign.action`, qs.stringify(params), { loading: false });
};

// 问题流程
export const processHandle = params => {
  return http.post(`/pages/problem!processHandle.action`, qs.stringify(params), { loading: false });
};

// 流程重新开始
export const actionFlow = params => {
  return http.post(`/pages/problem!actionFlow.action`, qs.stringify(params), { loading: false });
};

// 流程重新开始
export const findByIdCategorys = params => {
  return http.post(`/pages/event!findByIdCategorys.action?categoryId=` + params, qs.stringify({}), { loading: false });
};
